package com.yima.baseproj.module.sys.teacher;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yima.baseproj.aop.log.Logging;
import com.yima.baseproj.module.config.utils.PageUtil;
import com.yima.baseproj.module.config.utils.PageVo;
import com.yima.baseproj.module.sys.clazz.ClassEntity;
import com.yima.baseproj.module.sys.teacher.clazz.TeacherClassService;
import com.yima.baseproj.module.sys.teacher.model.TeacherSearchAddModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "教师管理")
@RequestMapping("/api/sys/teacher")
@RestController
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private TeacherClassService teacherClassService;

    @Logging(tags = "教师管理",value = "查询分页")
    @PreAuthorize("hasAuthority('sys:teacher:findAllByPage')")
    @ApiOperation("查询分页")
    @GetMapping("/findAllByPage")
    public Object findAllByPage(PageVo page, TeacherSearchAddModel searchModel) {
        Page<Map> pageData = teacherService.findAllByPage(PageUtil.initMpPage(page),searchModel);
        for (Map record : pageData.getRecords()){
            //根据教师id 获取班级信息
            List<ClassEntity> classEntityList = teacherClassService.getTeacherClass(Integer.parseInt(record.get("id").toString()));
            if (CollUtil.isNotEmpty(classEntityList)){
                record.put("classNameList",classEntityList.stream().map(item->item.getClassName()).distinct().collect(Collectors.toList()));
            }
        }
        return pageData;
    }

    @Logging(tags = "教师管理",value = "添加")
    @PreAuthorize("hasAuthority('sys:teacher:add')")
    @ApiOperation("添加")
    @PostMapping("/add")
    public void saveUser(@RequestBody @Validated TeacherSearchAddModel customAdmin) {
        teacherService.addTeacher(customAdmin);
    }

    @ApiOperation("根据id查询教师信息")
    @GetMapping("/findById/{id}")
    public TeacherSearchAddModel findById(@PathVariable int id){
        TeacherSearchAddModel teacherSearchAddModel = teacherService.findById(id);
        if (teacherSearchAddModel.getClassId()!=null){
            int classId = teacherSearchAddModel.getClassId();
            int kgId = teacherSearchAddModel.getKgId();
            int kgClass[] =new int[2];
            kgClass[0]=kgId;
            kgClass[1]=classId;
            teacherSearchAddModel.setKgClass(kgClass);
        }
        return teacherSearchAddModel;
    }

    @Logging(tags = "教师管理",value = "修改")
    @PreAuthorize("hasAuthority('sys:teacher:edit')")
    @ApiOperation("修改")
    @PostMapping("/edit")
    public void edit(@RequestBody @Validated TeacherSearchAddModel customAdmin) {
        teacherService.editTeacher(customAdmin);
    }

    @Logging(tags = "教师管理",value = "删除")
    @PreAuthorize("hasAuthority('sys:teacher:delByIds')")
    @ApiOperation("删除")
    @DeleteMapping(value = "/delByIds/{ids}")
    public void delByIds(@PathVariable Integer[] ids) {
        teacherService.delByIds(ids);
    }

}
